package com.soarsky.boteng;

import java.util.ArrayList;
import java.util.List;
/**
 * 表信息
 * @author Administrator
 *
 */
public class TableInfo {
	private final String ENDL = "\n";
	private final String TAB = "\t";
	private final String TAB2 = TAB + TAB;
	private final String TAB3 = TAB2 + TAB;
	private final String TAB4 = TAB2 + TAB2;
	private String name;
	private String primaryKey;//主键
	private String parserKey;//转换后的主键(小写)
	private List<ColumnInfo> columns;//表字段集合
	private List<FieldInfo> fields;//类属性集合

	public TableInfo(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPrimaryKey() {
		return primaryKey;
	}

	public void setPrimaryKey(String primaryKey) {
		this.primaryKey = primaryKey;
	}

	public List<ColumnInfo> getColumns() {
		return columns;
	}

	public void setColumns(List<ColumnInfo> columns) {
		this.columns = columns;
	}

	public String getParserKey() {
		return parserKey;
	}

	public void setParserKey(String parserKey) {
		this.parserKey = parserKey;
	}

	public void addColumn(ColumnInfo column) {
		if (columns == null)
			columns = new ArrayList<ColumnInfo>();
		if (!columns.contains(column)) {
			columns.add(column);
			String type = column.parseJavaType();
			String name = column.parseFieldName();
			addFiled(new FieldInfo(type, name,column.getRemarks()));
		}
	}

	public void addFiled(FieldInfo field) {
		if (fields == null)
			fields = new ArrayList<FieldInfo>();
		if (!fields.contains(field)) {
			fields.add(field);
		}

	}

	public List<FieldInfo> getFields() {
		return fields;
	}
    /**
     * 生成PO的属性
     * @return
     */
	public String getFieldsDeclareInfo() {
		StringBuffer sb = new StringBuffer();//属性
		StringBuffer sb1 = new StringBuffer();//get()和set()方法
		for (FieldInfo field : fields) {
			String name=field.getName();
			String type=field.getType();
			//属性
			sb.append(TAB+"private "+type+" "+name+";//"+field.getRemarks()+ENDL);
			//set()
			sb1.append(TAB+"public void set" + name.substring(0,1).toUpperCase() + name.substring( 1, name.length() ) + 
					"( " + type + " " + name + " ) {"+ENDL);
			        sb1.append(TAB);
			        sb1.append(TAB);
					sb1.append("this." + name + " = " + name + ";"+ENDL);
					sb1.append(TAB);
					sb1.append( "}"+ENDL);
			//get()
			sb1.append(TAB+"public " + type + " get" + 
							name.substring(0,1).toUpperCase() + name.substring( 1, name.length() ) + "(){"+ENDL);
					        sb1.append(TAB);
					        sb1.append(TAB);
							sb1.append( "return " + name + ";"+ENDL);
					        sb1.append(TAB);
							sb1.append( "}"+ENDL);	
		}
		return sb.toString()+sb1.toString();
	}

	public String getSelectStatement() {
		StringBuffer sb = new StringBuffer();
		sb.append("select ");
		sb.append(getColumnNames());
		sb.append(",count(*) over() countover");
		sb.append(" from " + name.toUpperCase());
		return sb.toString();
	}

	public String getInsertStatement() {
		StringBuffer sb = new StringBuffer();
		ColumnInfo col = null;
		sb.append("insert into " + name.toUpperCase());
		sb.append("( " + this.getColumnNames() + " ) values (");
		for (int i = 0; i < columns.size(); i++) {
			col = columns.get(i);
			if(col.getType().toUpperCase().equals("DATE")){
				sb.append("#{" + col.parseFieldName() +"}");
			}else{
				sb.append("#{" + col.parseFieldName() + ",jdbcType=" + col.parseJdbcType()+ "}");
			}
			if (i + 1 != columns.size()) {
				sb.append(",");
			}
			
		}
		sb.append(" )");
		return sb.toString();
	}

	public String getUpdateStatement() {
		StringBuffer sb = new StringBuffer();
		sb.append("update " + name.toUpperCase() + " set ");
		ColumnInfo col = null;
		for (int i = 0; i < columns.size(); i++) {
			col = columns.get(i);
			if(!col.getName().toLowerCase().equals(parserKey.toLowerCase())){
				if(col.getType().toUpperCase().equals("DATE")){
					sb.append(col.getName() + "=#{" + col.parseFieldName() +"}");
				}else{
					sb.append(col.getName() + "=#{" + col.parseFieldName() + ",jdbcType=" + col.parseJdbcType()+ "}");
				}
				if (i + 1 != columns.size()) {
					sb.append(",");
				}
			}
	
		}
		sb.append(" where " + primaryKey + "=#{"+parserKey+"}");
		return sb.toString();
	}

	public String getResultMap() {
		StringBuffer sb = new StringBuffer();
		for (ColumnInfo col : columns) {
			sb.append(TAB3);
			sb.append("<result property=\"" + col.parseFieldName() + "\" column=\"" + col.getName()
					+ "\" jdbcType=\"" + col.parseJdbcType() + "\" />");
			sb.append(ENDL);
		}
		return sb.toString();
	}
	public String getOtherCondition() {
		StringBuffer sb = new StringBuffer();
		for (ColumnInfo col : columns) {
			sb.append(TAB3);
			sb.append("<if test=\"" + col.parseFieldName() + " != null and " + col.parseFieldName() + " != '' \" > ");
			sb.append(" and "+ col.getName() +"=#{"+ col.parseFieldName()+",jdbcType=VARCHAR}");
			sb.append("</if>");
			sb.append(ENDL);
		}
		return sb.toString();
	}
	

	public String getFindByLike() {
		StringBuffer sb = new StringBuffer();
		for (ColumnInfo col : columns) {
			System.out.println(col.getName()+" "+col.getType()+" "+col.getType().contains("CHAR"));
			if (col.getType().contains("CHAR")) {
				sb.append(TAB4);
				sb.append("<if test=\"" + col.parseFieldName() + " != null and " + col.parseFieldName() + " != '' \" > ");
				sb.append(" and "+ col.getName() +" LIKE '%'||#{"+ col.parseFieldName()+",jdbcType=VARCHAR}||'%'");
				sb.append("</if>");
				sb.append(ENDL);
			}
		}
		return sb.toString();
	}

	private String getColumnNames() {
		StringBuffer sb = new StringBuffer();
		ColumnInfo column = null;
		for (int i = 0; i < columns.size(); i++) {
			column = columns.get(i);
			sb.append(column.parseFieldName());//别名
			if (i + 1 != columns.size()) {
				sb.append(",");
			}
		}
		return sb.toString();
	}
	/**
	 * 待修改
	 * @return
	 */
	public String getUpdateMapModel() {
		StringBuffer sb = new StringBuffer();
		sb.append("update " + name.toUpperCase() + " set "+primaryKey + "=#id:INTEGER#"+ENDL);
		sb.append(TAB+"<dynamic>"+ENDL);
		ColumnInfo col = null;
		for (int i = 0; i < columns.size(); i++) {
			col = columns.get(i);
			sb.append(TAB2+"<isPropertyAvailable property=\""+col.parseFieldName()+"\" prepend=\",\">"+ENDL);
			sb.append(TAB3+"<isNotNull property=\""+col.parseFieldName()+"\">"+ENDL);
			sb.append(TAB4+col.getName() + "=#" + col.parseFieldName() + ":" + col.parseJdbcType() + "#"+ENDL);
			sb.append(TAB3+"</isNotNull>"+ENDL);
			
			sb.append(TAB3+"<isNull property=\""+col.parseFieldName()+"\">"+ENDL);
			sb.append(TAB4+col.getName() + "=null "+ENDL);
			sb.append(TAB3+"</isNull>"+ENDL);
			sb.append(TAB2+"</isPropertyAvailable>"+ENDL);
		}
		sb.append(TAB+"</dynamic>"+ENDL);
		sb.append(TAB2+" where " + primaryKey + "=#id:INTEGER#");
		return sb.toString();	
	}
}
